<?php
/**
 * 
 */
namespace app\manage\controller\agenttypelevelcoinconfig;

use app\manage\controller\Base;

class Handle extends Base
{
    
    private $params;
    public function __construct(){
        parent::__construct();
        // token验证
        if(false == Base::checkToken()){
            $this->error('token error!');
        }
        // 参数去token
        $params = input('post.');
        unset($params['token']);
        $this->params = $params;
    }

    public function add(){
        $params = $this->params;
        $agent_type_id = $params['agent_type_id'];
        $coin_id = $params['coin_id'];
        $level_id = $params['level_id'];
        $config = $params['config'];

        $data = [
            'agent_type_id' => $agent_type_id,
            'coin_id' => $coin_id,
            'level_id' => $level_id,
            'config' => $config,
        ];

        $model = new \app\common\model\AgentTypeLevelCoinConfig;
        if($model->where(['agent_type_id'=>$agent_type_id,'level_id'=>$level_id,'coin_id'=>$coin_id])->find()){
            $this->error('该配置项已经存在');
        }else{
            $model->save($data);
            $CACHE = \app\common\cache\CacheData::getInstance('AgentTypeLevelCoinConfig');
            $CACHE -> updateCache();
            $this->success('添加成功');
        }

    }

    public function edit(){
        $params = $this->params;
        $id = $params['id'];
        $config = $params['config'];

        $model = new \app\common\model\AgentTypeLevelCoinConfig;

        $model->where(['id'=>$id])->update(['config'=>$config]);

        $CACHE = \app\common\cache\CacheData::getInstance('AgentTypeLevelCoinConfig');
        $CACHE -> updateCache();
        $this->success('修改成功');
    }    
    
    public function del(){
        $params = $this->params;
        $id = $params['id'];
        $model = new \app\common\model\AgentTypeLevelCoinConfig;
        if($model->where('id',$id)->delete()){
            $CACHE = \app\common\cache\CacheData::getInstance('AgentTypeLevelCoinConfig');
            $CACHE -> updateCache();
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}